home *** CD-ROM | disk | FTP | other *** search
/ Fritz: All Fritz / All Fritz.zip / All Fritz / FILES / DEMO_VGA / JULIA.LZH / JUL.ASM next >
Assembly Source File  |  1980-01-01  |  6KB  |  193 lines

  1. PAGE 255,132
  2.    ; Q
  3.    ;  Juline calculates a horozontal row of points for a quadratic
  4.    ;  Julia set.  The parameters are scaled to be close to the maximum
  5.    ;  magnitude for integers and integer arithmetic is used for speed.
  6.    ;      The unscaled Julia set is associated with the transformation
  7.    ;
  8.    ;     z  -->  z^2 - L  in the region abs(Re(z)) < 2 and abs(Im(z))<1
  9.    ;
  10.    ;  To scale to maximum size we use
  11.    ;
  12.    ;   x = Re(z) * 2^14    y = Im(z) * 2^15
  13.    ;   a = Re(L) * 2^14    b = Im(L) * 2^12
  14.    ;
  15.    ;  The transformation can then be written
  16.    ;
  17.    ;    x --> [4x^2/2^16  - (2^15 - 1)]  -  [ y^2/2^16 + (a - 2^15 + 1)]
  18.    ;    y -->  (8(xy - b*2^16))/2^16
  19.    ;
  20.    ;  The intermediate multiplication results are two words long.  Each
  21.    ;  2^16 indicates a simple full word shift.  The multiplications by
  22.    ;  4 and 8 indicate shifts through both words.  The complicated
  23.    ;  expressions are chosen so there is no chance of overflow
  24.    ;  until the last step. (The term 4x^2/2^16 could be larger than 2^15
  25.    ;  but becomes a regular signed integer when (2^15 - 1) is
  26.    ;  subtracted.  The canceling correction in the second bracketed
  27.    ;  expression ensures that it, too, will be a signed integer.)
  28.    ;
  29. only    segment
  30.     assume    cs:only, ds:data
  31.  
  32. picpar    struc    ; parameter vector in calling program
  33.       xi     dw ?    ;  initial x coord   DESTROYED BY JULINE
  34.       yi    dw ?    ; y coord for line
  35.       delx    dw ?    ; change in x for one pixel
  36.       a    dw ?    ;  function parameter -- real part (scaled)
  37.       b      dw ?    ;                        imag part (scaled)
  38.       maxit    dw ?    ; maximum function iterations for a pixel
  39.       c1    dw ?    ;if iterations LEFT < c1 use color 1
  40.       c2    dw ?    ;if iterations LEFT < c2 use color 2  (<c1)
  41.       nbyte dw ?   ; DESTROYED  BY JULINE  number of bytes to output
  42. picpar    ends
  43.  
  44. bigint    equ    7FFFH  ; 2^15 - 1
  45.  
  46. juline    proc    far  ; param are offsets of output and struct picpar
  47.  
  48.    ;  REGISTER USAGE FOR MAIN LOOP     SCREEN CALC
  49.    ;    ax    arith                   partial screen byte
  50.    ;    bx    y                       param offset then output offset
  51.    ;    cx    count of iterations
  52.    ;    dx     high mult
  53.    ;    bp    x                       point to param pointer
  54.    ;    si    a - bigint
  55.    ;      di    b
  56.  
  57. jul:    push    bp   ; must be saved
  58.     mov     bp,sp
  59.     add     bp,6    ; points to offset of picpar,
  60.             ;   bp + 2 points to output offset
  61.     mov    bx,[bp]                  ;offset of picpar
  62.     mov    si, [bx].a              ; keep a in si for whole routine
  63.     sub    si,bigint               ; shifted by bigint
  64.     mov    di, [bx].b              ; keep b for whole routine
  65.     mov    ax, [bx].delx       ;set xi
  66.     sub    [bx].xi, ax         ;  back as initialization
  67.  
  68. byteout:            ; loop for output byte
  69.  
  70.     ;  init for pixel loop
  71.     mov    cx, 3
  72.     push    cx        ;count of shifts to do
  73.     xor    cx, cx
  74.     push    cx
  75.  
  76. pixel:                     ; first initialize for iteration
  77.     push    bp            ;  about to be destroyed for iteration
  78.     mov     cx, [bx].maxit   ; number of iterations
  79.     mov    bp, [bx].delx
  80.     add    bp, [bx].xi      ;increment starting x
  81.     mov    [bx].xi, bp      ;save new one
  82.     mov bx, [bx].yi          ; starting y
  83.  
  84. iterate:             ; iterate the quadratic function
  85.     mov     ax, bp
  86.     imul    ax      ; square ax -- high word to dx
  87.     rcl    ax, 1    ; start first of two left shifts
  88.     rcl    dx, 1
  89.     rcl    ax, 1    ; low bits in ax do not matter
  90.     rcl    dx, 1
  91.     sub    dx, bigint      ;overflow prevention
  92.     xchg    dx, bp    ;save first term of new x and get old x
  93.     mov     ax, bx  ; prepare to mult by y
  94.     imul    dx    ; mult dx and ax (x and y)
  95.     sub    dx, di  ; subtract imaginary part of parameter
  96.     cmp    dx, 0FFFH    ; see if following three shifts
  97.     jg    enditer     ; will cause positive overflow,
  98.     cmp    dx, 0F000H    ; or
  99.     jl     enditer     ;negative overflow
  100.                 ; otherwise can shift
  101.     rcl    ax, 1           ; start first of three shifts
  102.     rcl    dx, 1
  103.     rcl    ax, 1
  104.     rcl    dx, 1
  105.     rcl    ax, 1
  106.     rcl    dx, 1    ; have new y
  107.     mov    ax, bx    ; get old y
  108.     mov    bx, dx    ; and replace by new one
  109.     imul    ax    ; square old y -- starting on 2nd term of new x
  110.     add    dx, si    ; include real part of parameter
  111.     sub    bp, dx    ; now have new x
  112.     jo    enditer
  113.     loop    iterate
  114.  
  115. enditer:
  116.     pop    bp
  117.     pop    ax        ; screen word
  118.     mov     bx, [bp]
  119.     cmp     cx, [bx].c1
  120.     jg    havecolor          ; backround color
  121.     inc    ax
  122.     cmp    cx, [bx].c2
  123.     jg    havecolor       ; color 1
  124.     inc    ax
  125.     cmp    cx, 0
  126.     jg    havecolor       ; color 2
  127.     inc    ax              ; color 3
  128. havecolor:
  129.     pop    cx        ; shift count, replace iter count
  130.     jcxz    bytedone
  131.     dec    cx
  132.     push    cx
  133.     sal    ax, 1     ; ready for two new bits
  134.     sal    ax, 1
  135.     push    ax        ; save intermediate screen word
  136.     jmp    pixel
  137. bytedone:
  138.     mov    bx,[bp] + 2       ; get output address
  139.     mov    [bx], al        ; save screen byte
  140.     inc    bx           ; advance
  141.     mov    [bp] +2, bx     ;   output address
  142.     mov    bx, [bp]         ; replace picpar offset
  143.     dec    [bx].nbyte    ; update number of words to generate
  144.     jz    cleanup
  145.     jmp     byteout
  146.  
  147. cleanup:
  148.     pop    bp        ; restore original base
  149.     ret    4
  150. juline    endp
  151.  
  152.  
  153.    ;  The driver for juline needs to set up the parameter struct and
  154.    ;  provide space for the output
  155.  
  156. data    segment
  157. param picpar <0a000h, 1 shl 14, 3 shl 10, 0 shl 14, 0 shl 12,7,5,3,4>
  158. scrstr    db     1160 dup(?)
  159. data     ends
  160.  
  161. ;picpar    struc    ; parameter vector in calling program
  162. ;      xi     dw -3 shl 13    ;  initial x coord   DESTROYED BY JULINE
  163. ;      yi    dw 0 shl 15    ; y coord for line
  164. ;      delx    dw 3 shl 9    ; change in x for one pixel
  165. ;      a    dw 0 shl 14    ;  function parameter -- real part (scaled)
  166. ;      b      dw 0 shl 12    ; imag part (scaled)
  167. ;      maxit    dw 7    ; maximum function iterations for a pixel
  168. ;      c1    dw 5    ;if iterations LEFT < c1 use color 1
  169. ;      c2    dw 3    ;if iterations LEFT < c2 use color 2  (<c1)
  170. ;      nbyte dw 4   ; DESTROYED  BY JULINE  number of bytes to output
  171. ;picpar    ends
  172.  
  173. driver:    ; PUSH PARAMETER ADDRESSES
  174.     mov     ax, seg data
  175.     mov     ds, ax
  176.     mov     ax, offset scrstr
  177.     push     ax
  178.     mov     ax, offset param
  179.     push     ax
  180.     call    far ptr jul
  181.  
  182.  
  183. only    ends
  184.  
  185. stack    segment   stack
  186.     assume ss:stack
  187.     dw     64 dup (?)
  188. stack     ends
  189.  
  190.     public cleanup, driver, enditer, havecolor, iterate, jul
  191.     public juline, param, pixel, scrstr, bytedone, byteout
  192.     end  driver
  193.